package com.itboye.hutouben.responsitory;

import android.content.Context;
import android.telephony.TelephonyManager;

import com.google.gson.reflect.TypeToken;
import com.itboye.hutouben.base.MyApplcation;
import com.itboye.hutouben.bean.ConfigAppBean;
import com.itboye.hutouben.bean.MyAddressBean;
import com.itboye.hutouben.bean.PersonDataBean;
import com.itboye.hutouben.bean.PersonDataTwoBean;
import com.itboye.hutouben.bean.UpdateVersionBean;
import com.itboye.hutouben.interfaces.IUserInfoInterface;
import com.itboye.hutouben.volley.BaseErrorListener;
import com.itboye.hutouben.volley.BaseNetRepository;
import com.itboye.hutouben.volley.BaseSuccessReqListener;
import com.itboye.hutouben.volley.ByJsonRequest;
import com.itboye.hutouben.volley.ICompleteListener;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2016/11/28.
 */

public class UserResponsitory extends BaseNetRepository implements
        IUserInfoInterface<PersonDataBean> {
    String api = "103";//接口版本号
    String loginTypeKey = "By_User_login";// 用户登录
    String loginZiDongTypeKey = "By_User_autoLogin";// 自动登录
    String register = "By_User_register";//用户注册
    private String sendVertificationCode = "By_SecurityCode_send";//发送验证码
    private String yanzhengVertificationCode = "By_SecurityCode_verify";//验证验证码
    private String updatePass = "By_User_updatePwd";//用户修改密码（手机号+验证码）
    private String updateXinxi = "By_User_update";//修改个人信息
    private String modify_pass = "By_User_updatePwdByOldPwd";//用户修改密码（通过旧密码）
    private String address_pass = "By_Address_add";//添加收货地址
    private String address_query = "By_Address_query";//查询收货地址
    private String delete_address = "By_Address_delete";//删除收货地址
    private String update_address = "By_Address_update";//更新收货地址
    private String moren_address = "By_Address_getDefault";//获取默认收货地址
    private String renzheng = "By_Driver_apply";//司机认证
    private String allAll = "By_ShoppingCart_bulkAdd";//批量添加购物车
    private String update = "By_Config_version";//版本更新
    private String configApp = "By_Config_app";//获取全局配置
    String updatePwdByPwd="By_User_updatePwdByOldPwd";//通过旧密码修改密码
    public UserResponsitory(ICompleteListener iCompleteListener) {
        super(iCompleteListener);
    }

    @Override
    public void login(String username, String pwd) {
        Type type = new TypeToken<PersonDataBean>() {
        }.getType();
        String apiVer = "104";
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("password", pwd);
        map.put("device_type", "android");
        map.put("country", "+86");
        map.put("role", "role_driver");
        map.put("device_token", getDeviceToken());
        (new ByJsonRequest.Builder<PersonDataBean>())
                .setTypeVerParamsAndReturnClass(loginTypeKey, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<PersonDataBean>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    @Override
    public void loginZiDong(String uid, String auto_login_code) {
        Type type = new TypeToken<PersonDataTwoBean>() {
        }.getType();
        String apiVer = "104";
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("auto_login_code", auto_login_code);
        (new ByJsonRequest.Builder<PersonDataTwoBean>())
                .setTypeVerParamsAndReturnClass(loginZiDongTypeKey, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<PersonDataTwoBean>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    @Override
    public void register(String username, String pwd, String country, String code, String from,String idcode) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "102";
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("password", pwd);
        map.put("country", "+86");
        map.put("code", code);
        map.put("from", from);
        map.put("idcode", idcode);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(register, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 发送验证码
     *
     * @param country   +86
     * @param mobile
     * @param code_type
     */
    @Override
    public void sendVerificationCode(String country, String mobile, String code_type) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("country", country);
        map.put("mobile", mobile);
        map.put("code_type", code_type);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(sendVertificationCode, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 验证验证码
     *
     * @param country
     * @param mobile
     * @param code
     * @param code_type
     */
    @Override
    public void YanZhengverificationCode(String country, String mobile, String code, String code_type) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("country", country);
        map.put("mobile", mobile);
        map.put("code", code);
        map.put("code_type", code_type);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(yanzhengVertificationCode, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 查询收货地址
     *
     * @param s_id 会话id[必须](登陆时保存的auth_login_code)
     * @param uid
     */
    @Override
    public void queryAddress(String s_id, String uid) {
        Type type = new TypeToken<List<MyAddressBean>>() {
        }.getType();
        String apiVer = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", s_id);
        map.put("uid", uid);
        (new ByJsonRequest.Builder<List<MyAddressBean>>())
                .setTypeVerParamsAndReturnClass(address_query, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<List<MyAddressBean>>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 删除收货地址
     *
     * @param uid
     * @param id
     */
    @Override
    public void deleteAddress(String s_id, String uid, String id) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", s_id);
        map.put("uid", uid);
        map.put("id", id);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(delete_address, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 获取默认收货地址
     *
     * @param uid
     */
    @Override
    public void getMoRenAddress(String s_id, String uid) {
        Type type = new TypeToken<MyAddressBean>() {
        }.getType();
        String apiVer = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", s_id);
        map.put("uid", uid);
        (new ByJsonRequest.Builder<MyAddressBean>())
                .setTypeVerParamsAndReturnClass(moren_address, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<MyAddressBean>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }


    /**
     * 修改密码
     *
     * @param country  国家[必须]
     * @param code     验证码[必须](itboye是万能验证码，测试其它请调用验证码接口获取)
     * @param mobile   手机号
     * @param password 新密码[必须]（6-32位）
     */
    @Override
    public void updatePass(String country, String code, String mobile, String password) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("country", country);
        map.put("code", code);
        map.put("mobile", mobile);
        map.put("password", password);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(updatePass, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 修改个人信息
     *
     * @param uid
     * @param types   (nickname:昵称;  sex:性别(0,1); sign:个性签名; email:邮箱； weixin：微信； company：公司； job_title：岗位；loc_country：所在国家编码； loc_area：地区)
     * @param content
     */
    @Override
    public void upadtaInformation(String uid, String types, String content) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "100";
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put(types, content);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(updateXinxi, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 修改密码（通过旧密码）
     *
     * @param uid
     * @param password
     * @param new_password
     */
    @Override
    public void modifyPass(String uid, String password, String new_password) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "100";
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("password", password);
        map.put("new_password", new_password);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(modify_pass, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    @Override
    public void addressAdd(String s_id, String uid, String contactname, String mobile, String postal_code, String province, String city, String area, String detailinfo, String defaults, String country, String country_id) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "102";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", s_id);
        map.put("uid", uid);
        map.put("contactname", contactname);
        map.put("mobile", mobile);
        map.put("postal_code", postal_code);
        map.put("province", province);
        map.put("city", city);
        map.put("area", area);
        map.put("detailinfo", detailinfo);
        map.put("default", defaults);
        map.put("country", country);
        map.put("country_id", country_id);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(address_pass, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 更新收货地址
     *
     * @param id
     * @param uid
     * @param contactname
     * @param mobile
     * @param postal_code
     * @param province
     * @param city
     * @param area
     * @param detailinfo
     * @param defaults
     * @param country
     * @param country_id
     */
    @Override
    public void updateAddress(String s_id, String id, String uid, String contactname, String mobile, String postal_code, String province, String city, String area, String detailinfo, String defaults, String country, String country_id) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", s_id);
        map.put("id", id);
        map.put("uid", uid);
        map.put("contactname", contactname);
        map.put("mobile", mobile);
        map.put("postal_code", postal_code);
        map.put("province", province);
        map.put("city", city);
        map.put("area", area);
        map.put("detailinfo", detailinfo);
        map.put("default", defaults);
        map.put("country", country);
        map.put("country_id", country_id);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(update_address, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 司机认证
     *
     * @param uid
     * @param id_number   身份证号
     * @param realname    真实姓名
     * @param id_certs    身份证图片id,多个英文逗号分割
     * @param driver_cert 行驶证图片id,暂一张
     */
    @Override
    public void sijiRenZheng(String uid, String id_number, String realname, String id_certs, String driver_cert) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "100";
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("id_number", id_number);
        map.put("realname", realname);
        map.put("id_certs", id_certs);
        map.put("driver_cert", driver_cert);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(renzheng, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 批量添加商品
     *
     * @param uid
     * @param id
     * @param sku_pkid
     * @param count
     */
    @Override
    public void addMallAll(String uid, String id, String sku_pkid, String count) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "100";
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("id", id);
        map.put("sku_pkid", sku_pkid);
        map.put("count", count);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(allAll, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    @Override
    public void banbenUpdate(String app_type) {
        Type type = new TypeToken<UpdateVersionBean>() {
        }.getType();
        String apiVer = "100";
        Map<String, Object> map = new HashMap<>();
        map.put("app_type", app_type);
        (new ByJsonRequest.Builder<UpdateVersionBean>())
                .setTypeVerParamsAndReturnClass(update, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<UpdateVersionBean>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    @Override
    public void getConfigApp() {
        Type type = new TypeToken<ConfigAppBean>() {
        }.getType();
        String apiVer = "100";
        Map<String, Object> map = new HashMap<>();
        (new ByJsonRequest.Builder<ConfigAppBean>())
                .setTypeVerParamsAndReturnClass(configApp, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<ConfigAppBean>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    @Override
    public void updatePwdByPwd(String uid, String oldPass, String newPass) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "100";

        Map<String, Object> map = new HashMap<>();
        map.put("uid",uid);
        map.put("password",oldPass);
        map.put("new_password",newPass);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(updatePwdByPwd, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    public String getDeviceToken() {
        TelephonyManager tm = (TelephonyManager) MyApplcation.ctx.getSystemService(Context.TELEPHONY_SERVICE);
        String DEVICE_ID = tm.getDeviceId();
        return DEVICE_ID;
    }
}
